let roomProto         = require('../../API/RoomProto');
let roomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';

cc.Class({
    extends: cc.Component,

    properties: {
        rowTitleNode: cc.Node,
        resultInfoItemArr: [cc.Node],
        lostNode: cc.Node,
        winNode: cc.Node,
        frameBg: cc.Node,
        btnNode: cc.Node,
        rowTitleGold: cc.Label,
    },

    onLoad () {
		this.tick = 30;
        let resultData = this.dialogParameters.resultData;
        let myChairID = this.dialogParameters.myChairID;
        this.showAnimation(resultData.winChairID === myChairID);

        if (resultData.winChairID === myChairID){
            this.lostNode.active = false;
            this.winNode.active = true;
        } 
		else {
            this.lostNode.active = true;
            this.winNode.active = false;
        }

		let item = cc.find('Item', this.node);
		for (let i = 0; i < 3; ++i) {
			if (resultData.nicknameArr[i] == null) {
				continue;
			}
			let node = cc.instantiate(item);
			node.parent = cc.find('Layout', this.node);
			node.active = true;

			let nameLabel = cc.find('name', node).getComponent(cc.Label);
			nameLabel.string = Global.UserModel.convertNickname(resultData.nicknameArr[i]);

			let idLabel = cc.find('id', node).getComponent(cc.Label);
			idLabel.string = 'ID:' + (resultData.idArr[i] || '');

			let headSprite = cc.find('head', node).getComponent(cc.Sprite);
			Global.CCHelper.updateSpriteFrame(resultData.headArr[i], headSprite);

			let card = cc.find('Card', node);
			for (let j = 0; j < resultData.allHandCards[i].length; ++j) {
				let cards = resultData.allHandCards[i][j].concat([0]);
				for (let k = 0; k < cards.length; ++k) {
					let cardNode = cc.instantiate(card);
					cardNode.parent = cc.find('Layout', node);
					cardNode.active = true;
					cardNode.y = 0;
					if (cards[k] != 0) {
						Global.CCHelper.updateSpriteFrame('Game/Cards/'+cards[k], cardNode.getComponent(cc.Sprite));
					}
					else {
						cardNode.width = 36;
					}
				}
			}

			let gold = resultData.winArr[i];
			if (gold > 0) {
				let winLabel = cc.find('win', node).getComponent(cc.Label);
				winLabel.node.active = true;
				winLabel.string = '+' + gold;
			}
			else {
				let loseLabel = cc.find('lose', node).getComponent(cc.Label);
				loseLabel.node.active = true;
				loseLabel.string = gold;
			}

			let restLabel = cc.find('rest', node).getComponent(cc.Label);
			restLabel.string = '剩余牌数：' + resultData.allCardArr[i].length;

			let bombLabel = cc.find('bomb', node).getComponent(cc.Label);
			bombLabel.string = '炸弹数：' + resultData.bombArr[i];

			cc.find('icon', node).active = (resultData.winChairID == i);
		}

		if (resultData.idArr[2] == null) { /* 两个玩家 */ 
			let restCards = resultData.allCardArr[2];
			let restLayout = cc.find('Rest/RestCards', this.node);
			let item = cc.find('card', restLayout);
			for (let card of restCards) {
				let node = cc.instantiate(item);
				Global.CCHelper.updateSpriteFrame('Game/Cards/'+card, node.getComponent(cc.Sprite));
				node.parent = restLayout;
				node.active = true;
			}
			cc.find('Rest', this.node).active = true;
		}

		if (this.dialogParameters.isBureauFull) {
			cc.find('btnNode', this.node).active = false;
			cc.find('ContinueBtn', this.node).active = true;
		}
    },

    onBtnClick: function (event, parameters) {
		Global.AudioManager.playCommonSoundClickButton();
		if(parameters === 'ready'){
			if (!this.dialogParameters.isBureauFull) {
				roomAPI.roomMessageNotify(roomProto.userReadyNotify());
			}
			else {
				if (this.dialogParameters.pdkMainDialog.showPDKAllResultFuc) {
					this.dialogParameters.pdkMainDialog.showPDKAllResultFuc();
				}
			}
			Global.DialogManager.destroyDialog('Game/PaoDeKuai/PDKResultDialog');
		}
		else if(parameters === 'exit'){
			if (this.dialogParameters.isBureauFull && this.dialogParameters.pdkMainDialog.showPDKAllResultFuc) {
				this.dialogParameters.pdkMainDialog.showPDKAllResultFuc();
			}
			Global.DialogManager.destroyDialog('Game/PaoDeKuai/PDKResultDialog');
		}
	},

    showAnimation: function (isWin) {
        {
            let title = null;
            if (isWin){
                title = this.winNode.getChildByName('titleBg');
            }else{
                title = this.lostNode.getChildByName('titleBg');
            }
            title.scale = 3;
            let action = cc.scaleTo(0.3, 1);
            action.easing(cc.easeIn(3));
            title.runAction(action);
        }
        {
            this.frameBg.opacity = 0;
            this.frameBg.runAction(cc.sequence([cc.delayTime(0.3), cc.fadeTo(0.3, 100)]));
        }
        {
            this.rowTitleNode.runAction(cc.sequence([cc.hide(), cc.delayTime(0.4), cc.show()]));
            for (let i = 0; i < 3; ++i){
                let node = this.resultInfoItemArr[i];
                node.opacity = 0;
                node.runAction(cc.sequence([cc.delayTime(0.7 + i * 0.2), cc.fadeIn(0.2)]));
            }
        }
        /*{
            this.btnNode.scale = 0.1;
            let action1 = cc.scaleTo(0.3, 1);
            action1.easing(cc.easeBackIn());
            this.btnNode.runAction(cc.sequence([cc.delayTime(1.5), action1]));
        }*/
    },

	update: function (dt) {
		if (this.dialogParameters.isDismissing()) { return; }
		this.tick -= dt;
		let second = Math.floor(this.tick);
		if (second <= 0) {
			cc.find('btnNode/Clock', this.node).active = false;
		}
		else {
			cc.find('btnNode/Clock/Label', this.node).getComponent(cc.Label).string = second;
		}
	},
});
